package com.hardroid.workload.ui.fragment;

import java.util.ArrayList;
import java.util.Date;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.hardroid.workload.R;
import com.hardroid.workload.utils.HardroidUtils;
import com.hardroid.workload.utils.IObsvFragmentReplacable;
import com.hardroid.workload.utils.Singleton;
import com.hardroid.workload.workcalc.Employer;

public class ObsvControlFragment extends Fragment {

	private Button nextButton;
	private Button prevButton;
	private TextView employerText;

	private Button nextDayButton;
	private Button prevDayButton;
	private TextView dayText;

	private Button generateButton;

	public ArrayList<Employer> lEmployer;
	public int currentEmployerId = 0;
	public int currentDayId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_obsv_control, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		nextButton = (Button) getView().findViewById(
			R.id.obsv_control_next_button);
		prevButton = (Button) getView().findViewById(
				R.id.obsv_control_previous_button);
		employerText = (TextView) getView().findViewById(
				R.id.obsv_control_employer_text);

		nextDayButton = (Button) getView().findViewById(
				R.id.obsv_control_next_day_button);
		prevDayButton = (Button) getView().findViewById(
				R.id.obsv_control_previous_day_button);
		dayText = (TextView) getView().findViewById(R.id.obsv_control_day_text);

		generateButton = (Button) getView().findViewById(
				R.id.obsv_control_generate_observation_button);

		generateButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// if
				// (lEmployer.get(currentEmployerId).sheet.getLatestObservation())
				lEmployer.get(currentEmployerId).sheet.addNewObservation();
				currentDayId++;
				updateView();
			}
		});

		nextButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (currentEmployerId < lEmployer.size() - 1) {
					currentEmployerId++;
					currentDayId = lEmployer.get(currentEmployerId).sheet.lObsvDay
							.size() - 1;
					updateView();
				}
			}
		});

		prevButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (currentEmployerId > 0) {
					currentEmployerId--;
					currentDayId = lEmployer.get(currentEmployerId).sheet.lObsvDay
							.size() - 1;
					updateView();
				}
			}
		});

		nextDayButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (currentDayId < lEmployer.get(currentEmployerId).sheet.lObsvDay
						.size() - 1) {
					currentDayId++;
					updateView();
				}
			}
		});

		prevDayButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (currentDayId > 0) {
					currentDayId--;
					updateView();
				}
			}
		});
		try{
			currentDayId = Singleton.currentProject.lEmployer.get(currentEmployerId).sheet.lObsvDay.size()-1;
		}catch(Exception e){
			
		}
		
		updateView();
	}

	public void setListEmployer(ArrayList<Employer> l) {
		lEmployer = l;
	}

	public void updateView() {
		nextButton.setEnabled(true);
		prevButton.setEnabled(true);
		if (currentEmployerId == 0) {
			prevButton.setEnabled(false);
		}

		if (currentEmployerId == lEmployer.size() - 1) {
			nextButton.setEnabled(false);
		}

		nextDayButton.setEnabled(true);
		prevDayButton.setEnabled(true);
		if (currentDayId == 0) {
			prevDayButton.setEnabled(false);
		}

		if (currentDayId == lEmployer.get(currentEmployerId).sheet.lObsvDay
				.size() - 1) {
			nextDayButton.setEnabled(false);
		}

		employerText.setText((currentEmployerId +1)+ " "
				+ lEmployer.get(currentEmployerId).name);
		Date obsvdate = lEmployer.get(currentEmployerId).sheet.lObsvDay
				.get(currentDayId).obsvDate;
		dayText.setText((currentDayId + 1) + " nth");

		if (!HardroidUtils.isDateToday(lEmployer.get(currentEmployerId).sheet
				.getLatestObservation().obsvDate)) {
			generateButton.setEnabled(true);
		} else {
			generateButton.setEnabled(false);
		}

		Log.i("ObsvControl", "replace " + currentEmployerId + ","
				+ currentDayId);
		((IObsvFragmentReplacable) getActivity()).replaceFragment(
				currentEmployerId, currentDayId);

	}
}
